using System.Collections;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GM : MonoBehaviour
{
	public static GM instance;

	public string language;

	public Animator AT_ShowMyGemForPlaying;

	public TextMeshProUGUI TXT_myGemAmount;

	public GameObject GO_TapToStart;

	public GameObject GO_Player;

	public Image IMG_PlayerHP;

	public Image IMG_EmenyHP;

	public GameObject GO_ClearedLevel;

	public TextMeshProUGUI TXT_ClearedLevel;

	public CanvasScaler[] canvasScalerList;

	public GameObject GO_BloodScreen;

	public GameObject GO_BTN_Pause;

	public GameObject GO_BTN_Resume;

	public GameObject GO_ShowingTheNewEquip;

	public GameObject[] GO_HowToContol;

	public bool playerIsActive;

	private float currentSlowMotionDuration;

	[HideInInspector]
	public float enemySpeedTimeByLevel;

	public int[] appearedCountOfbossList = new int[6];

	[HideInInspector]
	public int enemiesKilledCountInThisGame;

	[HideInInspector]
	public int score;

	private Coroutine CO_SlowMotion;

	private float tempTimeScale;

	private void Awake()
	{
		instance = this;
		GetDeviceLanguage();
		//AdMobCtrl.instance.GO_JustTXT_TestDevice.SetActive(false);
	}

	private IEnumerator Start()
	{
		Audio.instance.MusicSwitch(PlayerPrefs.GetString("musicSwitch"));
		yield return new WaitForSeconds(1f);
		Init();
		TimeReward.instance.Init();
	}

	public void Init()
	{
		StopAllCoroutines();
		SetScreen();
		Background.instance.TF_Golem.position = new Vector2(80f, 0f);
		Background.instance.SR_Planet.sprite = Sprites.instance.planetList[0];
		currentSlowMotionDuration = 0f;
		CanvasList.instance.TXT_HighestScore.text = PlayerPrefs.GetInt("highestScore").ToString();
		CanvasList.instance.TXT_Score.text = string.Empty;
		Audio.instance.music[0].Play();
		Audio.instance.SfxSwitch("off");
		Generator.instance.pool.DeActiveAllObject();
		AllObjectsInfo.Reset();
		GO_BTN_Pause.SetActive(false);
		GO_Player.SetActive(true);
		Player.instance.Init();
		Cam.instance.Init();
		Background.instance.Init();
		IMG_PlayerHP.fillAmount = Player.instance.hp * 0.1f;
		IMG_EmenyHP.fillAmount = 0f;
		ComboText.instance.Init();
		TouchEvent.instance.Init();
		GO_BloodScreen.SetActive(false);
		LevelController.instance.Init();
		CheckMoreUnlockingEquips();
		if (!CanvasList.instance.GO_Panel_Equip.activeSelf && !CanvasList.instance.GO_Panel_Menu.activeSelf)
		{
			GO_TapToStart.SetActive(true);
			CanvasList.instance.GO_PropsForTitle_Toggle(true);
		}
	}

	public void GoToTheHiddenBossStage()
	{
		score = 0;
		if (GO_BloodScreen.activeSelf)
		{
			Init();
		}
		Background.instance.TF_Golem.position = new Vector2(0f, 0f);
		CanvasList.instance.GO_PropsForTitle_Toggle(false);
		Background.instance.SR_Planet.sprite = Sprites.instance.planetList[1];
		Generator.instance.pool.DeActiveAllObject();
		AllObjectsInfo.Reset();
		appearedCountOfbossList = new int[6];
		Cam.instance.Init();
		Player.instance.Init();
		TouchEvent.instance.TouchEventStart();
		HiddenLevelController.instance.Init();
		enemiesKilledCountInThisGame = 0;
		GO_BTN_Pause.SetActive(true);
		Audio.instance.music[0].Play();
		Audio.instance.SfxSwitch("on");
	}

	public void TapToStart()
	{
		if (Player.isPlayVideo) return;
		score = 0;
		GameAdManager.StartGame(delegate
		{
			StartCoroutine(IE_TapToStart());
		});
	}

	private IEnumerator IE_TapToStart()
	{
		if (GO_BloodScreen.activeSelf)
		{
			Init();
		}
		CanvasList.instance.GO_PropsForTitle_Toggle(false);
		Generator.instance.pool.DeActiveAllObject();
		AllObjectsInfo.Reset();
		appearedCountOfbossList = new int[6];
		Cam.instance.Init();
		Player.instance.Init();
		TouchEvent.instance.TouchEventStart();
		LevelController.instance.Init();
		enemiesKilledCountInThisGame = 0;
		GO_BTN_Pause.SetActive(true);
		Audio.instance.music[0].Play();
		Audio.instance.SfxSwitch("on");
		yield return new WaitForSecondsRealtime(0.5f);
		if (PlayerPrefs.GetInt("firstPlay") == 0)
		{
			GO_HowToContol[0].SetActive(true);
			float temp = Time.timeScale;
			Time.timeScale = 0f;
			while (GO_HowToContol[0].activeSelf)
			{
				yield return new WaitForSecondsRealtime(0.1f);
			}
			Time.timeScale = temp;
		}
		CanvasList.instance.TXT_Score.text = "0";
	}

	private void SetScreen()
	{
		Screen.sleepTimeout = -1;
		Cam.instance.screenRatio = (float)Screen.height / (float)Screen.width;
		PlayerPrefs.SetFloat("screenRatio", Cam.instance.screenRatio);
		if (Cam.instance.screenRatio > 0.57f)
		{
			for (int i = 0; i < canvasScalerList.Length; i++)
			{
				canvasScalerList[i].matchWidthOrHeight = 0f;
			}
		}
		else
		{
			for (int j = 0; j < canvasScalerList.Length; j++)
			{
				canvasScalerList[j].matchWidthOrHeight = 1f;
			}
		}
	}

	public void Restart()
	{
		SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
	}

	public void SlowMotion(float timeScale, float duration)
	{
		if (currentSlowMotionDuration < duration && duration > 0.1f)
		{
			currentSlowMotionDuration = duration;
			if (CO_SlowMotion != null)
			{
				StopCoroutine(CO_SlowMotion);
			}
			CO_SlowMotion = StartCoroutine(IE_SlowMotion(timeScale, duration));
		}
	}

	private IEnumerator IE_SlowMotion(float timeScale, float duration)
	{
		if (duration > 2f)
		{
			Time.timeScale = 0f;
			yield return new WaitForSecondsRealtime(0.2f);
		}
		Time.timeScale = timeScale;
		Time.fixedDeltaTime = Time.timeScale * 0.02f;
		yield return new WaitForSecondsRealtime(duration);
		Time.timeScale = 1f;
		Time.fixedDeltaTime = Time.timeScale * 0.02f;
		currentSlowMotionDuration = 0f;
	}

	public void PauseGame()
	{
		GO_BTN_Pause.SetActive(false);
		StartCoroutine(IE_PauseGame());
	}

	private IEnumerator IE_PauseGame()
	{
		while (Time.timeScale != 1f)
		{
			yield return null;
		}
		GameAdManager.Pause();
		GO_BTN_Resume.SetActive(true);
		tempTimeScale = Time.timeScale;
		Time.timeScale = 0f;
	}

	public void ResumeGame()
	{
		Time.timeScale = tempTimeScale;
		GO_BTN_Resume.SetActive(false);
		GO_BTN_Pause.SetActive(true);
	}

	public void CheckMoreUnlockingEquips()
	{
		if (PlayerPrefs.GetInt("currentUnlockedWeaponNumber") + 1 < Generator.instance.pool.SCRT_WeaponAbilityList.Length && PlayerPrefs.GetInt("playerGem") >= Generator.instance.pool.SCRT_WeaponAbilityList[PlayerPrefs.GetInt("currentUnlockedWeaponNumber") + 1].price)
		{
			GO_ShowingTheNewEquip.SetActive(true);
			GO_ShowingTheNewEquip.GetComponent<ShowingTheNewEquip>().Show("Weapon", Generator.instance.pool.SCRT_WeaponAbilityList[PlayerPrefs.GetInt("currentUnlockedWeaponNumber") + 1].GetComponent<SpriteRenderer>().sprite, Generator.instance.pool.SCRT_WeaponAbilityList[PlayerPrefs.GetInt("currentUnlockedWeaponNumber") + 1].equipName, Generator.instance.pool.SCRT_WeaponAbilityList[PlayerPrefs.GetInt("currentUnlockedWeaponNumber") + 1].equipType);
			PlayerPrefs.SetInt("currentUnlockedWeaponNumber", PlayerPrefs.GetInt("currentUnlockedWeaponNumber") + 1);
		}
		else if (PlayerPrefs.GetInt("currentUnlockedHelmNumber") + 1 < AllEquipInfo.instance.helmList.Length && PlayerPrefs.GetInt("playerGem") >= AllEquipInfo.instance.helmList[PlayerPrefs.GetInt("currentUnlockedHelmNumber") + 1].price)
		{
			GO_ShowingTheNewEquip.SetActive(true);
			GO_ShowingTheNewEquip.GetComponent<ShowingTheNewEquip>().Show("Helm", Sprites.instance.helmSpriteList[PlayerPrefs.GetInt("currentUnlockedHelmNumber") + 1], AllEquipInfo.instance.helmList[PlayerPrefs.GetInt("currentUnlockedHelmNumber") + 1].equipName, AllEquipInfo.instance.helmList[PlayerPrefs.GetInt("currentUnlockedHelmNumber") + 1].equipType);
			PlayerPrefs.SetInt("currentUnlockedHelmNumber", PlayerPrefs.GetInt("currentUnlockedHelmNumber") + 1);
		}
	}

	public void UnlockTheCloak()
	{
		if (PlayerPrefs.GetInt("currentUnlockedCloakNumber") + 1 < VariousInfo.cloakInfo.Length)
		{
			GO_ShowingTheNewEquip.SetActive(true);
			GO_ShowingTheNewEquip.GetComponent<ShowingTheNewEquip>().Show("Cloak", Sprites.instance.etcSpriteList[6], VariousInfo.cloakInfo[PlayerPrefs.GetInt("currentUnlockedCloakNumber") + 1].name, "斗篷");
			PlayerPrefs.SetInt("currentUnlockedCloakNumber", PlayerPrefs.GetInt("currentUnlockedCloakNumber") + 1);
		}
	}

	private void GetDeviceLanguage()
	{
		language = Application.systemLanguage.ToString();
	}
}
